/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.server.buz;

import ChatSoft.model.User;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author KID
 */
public class UserBussness {

    XmlHandle xmlHandle;

    public UserBussness() {
        xmlHandle = new XmlHandle();
    }

    public Boolean createNewUser(User user) {

        if (isUserExist(user.getUserName())) {
            return false;
        }
        xmlHandle.WriteUserToXML(user);
        return true;
    }

    public User userLogin(String userName, String password) {
        if (getAllUser().contains(userName)) {
            User user = getUserInfo(userName);
            if (user != null && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    public Boolean removeFriend(String friendName, String userName) {
        return true;
    }

    private Boolean isUserExist(String userName) {
        List<String> userList = getAllUser();
        if (userList != null && userList.contains(userName)) {
            return true;
        }
        return false;
    }

    public List getAllUser() {
        List<String> userList = new ArrayList<String>();
        File directory = new File("src/chatsoft/server/userplace");
        String filename[] = directory.list();
        for (int i = 0; i < filename.length; i++) {
            userList.add(filename[i].substring(0, filename[i].length() - 4));
        }
        return userList;
    }

    public User getUserInfo(String userName) {
        User user = xmlHandle.xmlToUser("src/chatsoft/server/userplace/" + userName + ".xml");
        return user;
    }
    public Boolean updateAccount(User user)
    {
        try
        {
            xmlHandle.WriteUserToXML(user);
            return true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        
    }
}
